package com.richapp.launcher.service;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;

import com.richapp.launcher.R;
import com.richapp.launcher.busevent.EventPhoneAction;
import com.richapp.launcher.busevent.EventServiceDestroy;
import com.richapp.launcher.busevent.EventUnbindBox;
import com.richapp.launcher.control.PageControl;
import com.richapp.launcher.database.system.dao.UserLoginDao;
import com.richapp.launcher.net.mobile.server.UserServer;
import com.richapp.launcher.receiver.LauncherMessageReceiver;
import com.richapp.launcher.receiver.TimeTickReceiver;
import com.richapp.launcher.utils.Tools;

import de.greenrobot.event.EventBus;

public class UserService extends Service{

    private static final String TAG = UserService.class.getName();
    private UserServer userServer = null;

    private LauncherMessageReceiver launcherMessageReceiver;
    private TimeTickReceiver timeTickReceiver;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        EventBus.getDefault().register(this);

        //清除登陆记录
        new UserLoginDao(this).delAll();

        //启动用户连接服务
        userServer = UserServer.getInstance();
        userServer.setRestartFlag(true);
        userServer.setContext(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                userServer.start();
                Log.d(TAG,"用户服务启动");
            }
        }).start();

        //保持服务
        Notification notification = new Notification();
        startForeground(2, notification);
        //Toast.makeText(this, "用户服务启动", Toast.LENGTH_LONG).show();


        //记载launcher广播
        launcherMessageReceiver = new LauncherMessageReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LauncherMessageReceiver.ACTION_PHONE_REFUSE);
        intentFilter.addAction(LauncherMessageReceiver.ACTION_SCREEN_SAVER_START);
        registerReceiver(launcherMessageReceiver,intentFilter);

        //广播监测服务启动
        timeTickReceiver = new TimeTickReceiver();
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(timeTickReceiver, intentFilter2);
    }

    public void onEventMainThread(EventUnbindBox event){
        if(event.isRestartFlag()){
            Toast.makeText(this,"机顶盒"+event.getBoxCpuId()+"已重置,即将重启!",Toast.LENGTH_SHORT).show();
            //机顶盒重启
            Tools.restartSystem(this);
        }else{
            Toast.makeText(this,"机顶盒"+event.getBoxCpuId()+"已重置",Toast.LENGTH_SHORT).show();
        }
    }

    public void onEventMainThread(EventPhoneAction event){
        switch (event.getActionType()){

            case EventPhoneAction.PHONE_ACTION_TYPE_ADD:
                Tools.toastTip(this, R.drawable.phone_connect);

                Intent intentPhoneAdd1 = new Intent();
                intentPhoneAdd1.setAction(LauncherMessageReceiver.ACTION_PHONE_ADD);
                intentPhoneAdd1.putExtra("phoneUid", event.getPhoneUid());
                intentPhoneAdd1.putExtra("phoneName", event.getPhoneName());
                sendBroadcast(intentPhoneAdd1);

                //如果屏幕不亮，自动点亮屏幕
                PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
                if(!powerManager.isScreenOn()){
                    PageControl.getInstance().power();
                }

                break;

            case EventPhoneAction.PHONE_ACTION_TYPE_DEL:
                Tools.toastTip(this,R.drawable.phone_disconnect);

                Intent intentPhoneDel1 = new Intent();
                intentPhoneDel1.setAction(LauncherMessageReceiver.ACTION_PHONE_DEL);
                intentPhoneDel1.putExtra("phoneUid", event.getPhoneUid());
                sendBroadcast(intentPhoneDel1);
                break;

            case EventPhoneAction.PHONE_ACTION_TYPE_BIND:
                Tools.toastTip(this,R.drawable.phone_bind);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(launcherMessageReceiver);
        unregisterReceiver(timeTickReceiver);
        userServer.stop();

        EventBus.getDefault().post(new EventServiceDestroy(UserService.class));
    }
}
